package com.qire.manhua.controller;

import android.graphics.Rect;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.OSUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.databinding.FragmentComicDetailIntroductionBinding;
import com.qire.manhua.dialog.ComicCommentOptionMenu;
import com.qire.manhua.dialog.CommentComplaintDialog;
import com.qire.manhua.fragment.ComicDetailIntroductionFragment;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.ComicDetailIntroductionViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDetailIntroductionViewModel.ComicDetailCommentItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDetailIntroductionViewModel.ComicDetailRecommendRankItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDetailIntroductionViewModel.ComicDetailRecommendLikeItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.databinding.ViewDataBinding;

/**
 * 事件控制器
 * 漫画详情页->简介
 */
public class ComicDetailIntroductionController extends EventController<ComicDetailIntroductionFragment> implements AutoVariable {

    private ComicDetailIntroductionViewModel viewModel;

    public ComicDetailIntroductionController(@NonNull ComicDetailIntroductionFragment lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        FragmentComicDetailIntroductionBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setComicDetailIntroductionController(this);
        }
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.comicDetailIntroductionController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicDetailIntroductionViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**
     * 写评论*/
    public void writeCommentBtnOnClick(View view) {
//        Logger.d("写评论");
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicCommentPage)
                .withData("comicBookEntity", viewModel.comicBookEntity())
                .navigation();
    }

    /**
     * 查看所有评论*/
    public void moreCommentBtnOnClick(View view) {
//        Logger.d("更多评论");

        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicCommentListPage)
                .withData("comicBookEntity", viewModel.comicBookEntity())
                .navigation();
    }

    /**
     * 查看分类*/
    public void gotoClassifyBtnOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.HomePage)
                .withData("tabIndex", PageTabIndex.TAB_INDEX_HOME_CLASSIFICATION)
                .withData("subTabIndex", viewModel.rankTypeId())
                .go();
    }

    /**
     * 排行榜ItemOnClick
     * @param view
     */
    public void rankItemOnClick(View view) {
        ComicDetailRecommendRankItemViewModel itemViewModel = (ComicDetailRecommendRankItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicQuickReadPage)
                .withData("comicBookEntity", new ComicBookEntity(itemViewModel.bookId, itemViewModel.bookName))
                .navigation();
    }

    /**
     * 猜你喜欢换一批
     */
    public void refreshBatchBtnOnClick(View view) {
        viewModel.loadRandomRecommend();
    }

    //region #评论区按钮事件响应 begin
    /**
     * 给喜欢的评论点赞按钮
     * */
    public void giveThumbsUpBtnOnClick(View view) {
        ComicDetailCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), ComicDetailCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            if(itemViewModel.isLike()) {
                ToastHelper.showToast("您已经点过赞了");
                return;
            }
            itemViewModel.voteToGiveLike();
        }
    }

    /**
     * 评论单击事件 和 评论回复按钮事件
     * @param view
     */
    public void commentReplyOnClick(View view) {
        ComicDetailCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), ComicDetailCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicCommentReplyPage)
                    .withData("comicComment", itemViewModel.comicComment())
                    .withData("comicBookEntity", viewModel.comicBookEntity())
                    .navigation();
        }
    }

    /**
     * 评论的举报按钮单击事件
     * */
    public void complaintBtnOnClick(View view) {
        ComicDetailCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), ComicDetailCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            CommentComplaintDialog dialog = new CommentComplaintDialog(view.getContext());
            dialog.setCommentInfo(itemViewModel.userName(), itemViewModel.content());
            dialog.setListener((int number, String text) -> itemViewModel.complaint(number, text));
            dialog.show();
        }
    }

    /**
     * 长按评论响应*/
    public boolean commentItemOnLongClick(View view) {
        ComicDetailCommentItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), ComicDetailCommentItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            ComicCommentOptionMenu menu = new ComicCommentOptionMenu(view.getContext());
            menu.setComplaintListener(v -> {
                v.setTag(itemViewModel);
                complaintBtnOnClick(v);
                v.setOnClickListener(null);
            }).setReplyListener(v -> {
                v.setTag(itemViewModel);
                commentReplyOnClick(v);
                v.setOnClickListener(null);
            }).setCopyListener(v -> {
                String text = itemViewModel.content();
                OSUtil.copyToClipboard(view.getContext(), "Label", text);
                ToastHelper.showToast("复制成功");
                v.setOnClickListener(null);
            }).show();
            return true;
        }
        return false;
    }
    //endregion #评论区按钮事件响应  end

    /**
     * 猜你喜欢项目点击*/
    public void recommendItemOnClick(View view) {

        ComicDetailRecommendLikeItemViewModel itemViewModel = (ComicDetailRecommendLikeItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicQuickReadPage)
                .withData("comicBookEntity", new ComicBookEntity(itemViewModel.bookId, itemViewModel.bookName))
                .navigation();

        // 行为数据统计
        ComicActionRecorder.build(ComicActionRecorder.count_readpage_like)
                .putParam("图片顺序", String.valueOf(itemViewModel.serialNumber))
                .setComicInfo( itemViewModel.bookId , itemViewModel.bookName)
                .doRecorde(view.getContext());
    }

    boolean isVisible = false;
    //监听滑到猜你喜欢
    public void rootViewOnScrollChange(NestedScrollView view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        FragmentComicDetailIntroductionBinding viewDataBinder = owner.viewDataBinder();
        boolean tempIsVisible = viewDataBinder.recommendLike.getLocalVisibleRect(new Rect());
        if(isVisible == tempIsVisible) {
            return;
        }
        isVisible = tempIsVisible;
        if(isVisible) {
            ComicBookEntity comicBookEntity = viewModel.comicBookEntity();
            ComicActionRecorder.build(ComicActionRecorder.count_readpage_like_show)
                    .setComicInfo(comicBookEntity.id, comicBookEntity.bookName)
                    .doRecorde(view.getContext());
        }
    }

}
